package com.ssm.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebFilter("/*")
public class CorsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        // 允许哪些域名访问当前的程序
        resp.setHeader("Access-Control-Allow-Origin","*");
        // 允许采用哪些请求方式访问当前的程序
        resp.setHeader("Access-Control-Allow-Methods","*");
        // 允许发送请求的时候携带哪些请求头
        resp.setHeader("Access-Control-Allow-Headers","*");
        chain.doFilter(req,resp);
    }
}
